package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.db.DBEngineGame;
import org.gdstash.db.DBFactionReputation;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class GDCharFactionPane extends AdjustablePanel {
    private static final int FACTION_NONE = -1;
    private static final int FACTION_DEVILS_CROSSING = 1;
    private static final int FACTION_AETHERIALS = 2;
    private static final int FACTION_CHTHONIANS = 3;
    private static final int FACTION_CRONLEYS_GANG = 4;
    private static final int FACTION_BEASTS = 5;
    private static final int FACTION_ROVERS = 6;
    private static final int FACTION_NEUTRAL = 7;
    private static final int FACTION_HOMESTEAD = 8;
    private static final int FACTION_CORRIGAN_MINE = 9;
    private static final int FACTION_THE_OUTCAST = 10;
    private static final int FACTION_DEATHS_VIGIL = 11;
    private static final int FACTION_UNDEAD = 12;
    private static final int FACTION_BLACK_LEGION = 13;
    private static final int FACTION_KYMONS_CHOSEN = 14;
    private static final int FACTION_COVEN_UGDENBOG = 15;
    private static final int FACTION_BARROWHOLM = 16;
    private static final int FACTION_MALMOUTH_RESISTANCE = 17;
    private static final int FACTION_AETHERIAL_VANGUARD = 18;
    private static final int FACTION_CULT_BYSMIEL = 19;
    private static final int FACTION_CULT_DREEG = 20;
    private static final int FACTION_CULT_SOLAEL = 21;
    private static final int FACTION_ELDRITCH_HORRORS = 22;
    public static final int[] FACTIONS_VANILLA;
    public static final int[] FACTIONS_MALMOUTH;
    public static final int[] FACTIONS_GODS;
    public static int[] FACTIONS;
    private static final int TYPE_REPUTATION_ALL = 1;
    private static final int TYPE_REPUTATION_POSITIVE = 2;
    private static final int TYPE_REPUTATION_NEGATIVE = 3;
    private static int[] minReputationLevels;
    private static String[] factionNegative;
    private static String[] factionPositive;
    private static String[] factionAll;
    private JLabel lblSurvivors;
    private JLabel lblRovers;
    private JLabel lblHomestead;
    private JLabel lblBlackLegion;
    private JLabel lblDeathsVigil;
    private JLabel lblKymonsChosen;
    private JLabel lblOutcast;
    private JLabel lblCovenUgdenbog;
    private JLabel lblBarrowholm;
    private JLabel lblMalmouth;
    private JLabel lblCultBysmiel;
    private JLabel lblCultDreeg;
    private JLabel lblCultSolael;
    private JLabel lblUndead;
    private JLabel lblBeasts;
    private JLabel lblAetherials;
    private JLabel lblChthonians;
    private JLabel lblOutlaws;
    private JLabel lblAetherialVanguard;
    private JLabel lblEldritchHorrors;
    private JComboBox<String> cbSurvivors;
    private JComboBox<String> cbRovers;
    private JComboBox<String> cbHomestead;
    private JComboBox<String> cbBlackLegion;
    private JComboBox<String> cbDeathsVigil;
    private JComboBox<String> cbKymonsChosen;
    private JComboBox<String> cbOutcast;
    private JComboBox<String> cbCovenUgdenbog;
    private JComboBox<String> cbBarrowholm;
    private JComboBox<String> cbMalmouth;
    private JComboBox<String> cbCultBysmiel;
    private JComboBox<String> cbCultDreeg;
    private JComboBox<String> cbCultSolael;
    private JComboBox<String> cbUndead;
    private JComboBox<String> cbBeasts;
    private JComboBox<String> cbAetherials;
    private JComboBox<String> cbChthonians;
    private JComboBox<String> cbOutlaws;
    private JComboBox<String> cbAetherialVanguard;
    private JComboBox<String> cbEldritchHorrors;
    private int[] reputations;
    private int[] reputationTypes;
    private JComboBox[] cbReputations;
    private boolean skipListener;

    static {
        FACTIONS_VANILLA = new int[]{1, 6, 8, 13, 11, 14, 10, -1, -1, -1, -1, -1, -1, 12, -1, 2, 3, 4, -1, -1};
        FACTIONS_MALMOUTH = new int[]{1, 6, 8, 13, 11, 14, 10, 15, 16, 17, -1, -1, -1, 12, 5, 2, 3, 4, 18, -1};
        FACTIONS_GODS = new int[]{1, 6, 8, 13, 11, 14, 10, 15, 16, 17, 19, 20, 21, 12, 5, 2, 3, 4, 18, 22};
        GDCharFactionPane.FACTIONS = null;
        if (GDStashFrame.expansionForgottenGods) {
            GDCharFactionPane.FACTIONS = GDCharFactionPane.FACTIONS_GODS;
        } else if (GDStashFrame.expansionAshesOfMalmouth) {
            GDCharFactionPane.FACTIONS = GDCharFactionPane.FACTIONS_MALMOUTH;
        } else {
            GDCharFactionPane.FACTIONS = GDCharFactionPane.FACTIONS_VANILLA;
        }
        GDCharFactionPane.minReputationLevels = new int[10];
        GDCharFactionPane.factionAll = new String[10];
        GDCharFactionPane.factionNegative = new String[5];
        GDCharFactionPane.factionPositive = new String[6];
        initReputationLevels();
        loadCBText();
    }

    private static void initReputationLevels() {
        boolean defaultRep = true;
        final DBEngineGame game = DBEngineGame.get();
        if (game != null) {
            final List<DBFactionReputation> values = game.getReputationList();
            if (values.size() >= 8) {
                defaultRep = false;
                GDCharFactionPane.minReputationLevels[0] = values.get(0).getValue();
                GDCharFactionPane.minReputationLevels[1] = values.get(1).getValue();
                GDCharFactionPane.minReputationLevels[2] = values.get(2).getValue();
                GDCharFactionPane.minReputationLevels[3] = -1;
                GDCharFactionPane.minReputationLevels[4] = 0;
                GDCharFactionPane.minReputationLevels[5] = 1;
                GDCharFactionPane.minReputationLevels[6] = values.get(4).getValue();
                GDCharFactionPane.minReputationLevels[7] = values.get(5).getValue();
                GDCharFactionPane.minReputationLevels[8] = values.get(6).getValue();
                GDCharFactionPane.minReputationLevels[9] = values.get(7).getValue();
            }
        }
        if (defaultRep) {
            GDCharFactionPane.minReputationLevels[0] = -20000;
            GDCharFactionPane.minReputationLevels[1] = -8000;
            GDCharFactionPane.minReputationLevels[2] = -1500;
            GDCharFactionPane.minReputationLevels[3] = -1;
            GDCharFactionPane.minReputationLevels[4] = 0;
            GDCharFactionPane.minReputationLevels[5] = 1;
            GDCharFactionPane.minReputationLevels[6] = 1500;
            GDCharFactionPane.minReputationLevels[7] = 5000;
            GDCharFactionPane.minReputationLevels[8] = 10000;
            GDCharFactionPane.minReputationLevels[9] = 25000;
        }
    }

    public GDCharFactionPane() {
        this.reputations = new int[GDCharFactionPane.FACTIONS.length];
        this.adjustUI();
        (this.cbReputations = new JComboBox[GDCharFactionPane.FACTIONS.length])[0] = this.cbSurvivors;
        this.cbReputations[1] = this.cbRovers;
        this.cbReputations[2] = this.cbHomestead;
        this.cbReputations[3] = this.cbBlackLegion;
        this.cbReputations[4] = this.cbDeathsVigil;
        this.cbReputations[5] = this.cbKymonsChosen;
        this.cbReputations[6] = this.cbOutcast;
        this.cbReputations[7] = this.cbCovenUgdenbog;
        this.cbReputations[8] = this.cbBarrowholm;
        this.cbReputations[9] = this.cbMalmouth;
        this.cbReputations[10] = this.cbCultBysmiel;
        this.cbReputations[11] = this.cbCultDreeg;
        this.cbReputations[12] = this.cbCultSolael;
        this.cbReputations[13] = this.cbUndead;
        this.cbReputations[14] = this.cbBeasts;
        this.cbReputations[15] = this.cbAetherials;
        this.cbReputations[16] = this.cbChthonians;
        this.cbReputations[17] = this.cbOutlaws;
        this.cbReputations[18] = this.cbAetherialVanguard;
        this.cbReputations[19] = this.cbEldritchHorrors;
        (this.reputationTypes = new int[GDCharFactionPane.FACTIONS.length])[0] = 2;
        this.reputationTypes[1] = 2;
        this.reputationTypes[2] = 2;
        this.reputationTypes[3] = 2;
        this.reputationTypes[4] = 1;
        this.reputationTypes[5] = 1;
        this.reputationTypes[6] = 1;
        this.reputationTypes[7] = 2;
        this.reputationTypes[8] = 1;
        this.reputationTypes[9] = 2;
        this.reputationTypes[10] = 2;
        this.reputationTypes[11] = 2;
        this.reputationTypes[12] = 2;
        this.reputationTypes[13] = 3;
        this.reputationTypes[14] = 3;
        this.reputationTypes[15] = 3;
        this.reputationTypes[16] = 3;
        this.reputationTypes[17] = 3;
        this.reputationTypes[18] = 3;
        this.reputationTypes[19] = 3;
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblSurvivors).addComponent(this.lblRovers).addComponent(this.lblHomestead).addComponent(this.lblBlackLegion).addComponent(this.lblDeathsVigil).addComponent(this.lblKymonsChosen).addComponent(this.lblOutcast).addComponent(this.lblCovenUgdenbog).addComponent(this.lblBarrowholm).addComponent(this.lblMalmouth).addComponent(this.lblCultBysmiel).addComponent(this.lblCultDreeg).addComponent(this.lblCultSolael).addComponent(this.lblUndead).addComponent(this.lblBeasts).addComponent(this.lblAetherials).addComponent(this.lblChthonians).addComponent(this.lblOutlaws).addComponent(this.lblAetherialVanguard).addComponent(this.lblEldritchHorrors)).addGroup(layout.createParallelGroup().addComponent(this.cbSurvivors).addComponent(this.cbRovers).addComponent(this.cbHomestead).addComponent(this.cbBlackLegion).addComponent(this.cbDeathsVigil).addComponent(this.cbKymonsChosen).addComponent(this.cbOutcast).addComponent(this.cbCovenUgdenbog).addComponent(this.cbBarrowholm).addComponent(this.cbMalmouth).addComponent(this.cbCultBysmiel).addComponent(this.cbCultDreeg).addComponent(this.cbCultSolael).addComponent(this.cbUndead).addComponent(this.cbBeasts).addComponent(this.cbAetherials).addComponent(this.cbChthonians).addComponent(this.cbOutlaws).addComponent(this.cbAetherialVanguard).addComponent(this.cbEldritchHorrors));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblSurvivors).addComponent(this.cbSurvivors)).addGroup(layout.createParallelGroup().addComponent(this.lblRovers).addComponent(this.cbRovers)).addGroup(layout.createParallelGroup().addComponent(this.lblHomestead).addComponent(this.cbHomestead)).addGroup(layout.createParallelGroup().addComponent(this.lblBlackLegion).addComponent(this.cbBlackLegion)).addGroup(layout.createParallelGroup().addComponent(this.lblDeathsVigil).addComponent(this.cbDeathsVigil)).addGroup(layout.createParallelGroup().addComponent(this.lblKymonsChosen).addComponent(this.cbKymonsChosen)).addGroup(layout.createParallelGroup().addComponent(this.lblOutcast).addComponent(this.cbOutcast)).addGroup(layout.createParallelGroup().addComponent(this.lblCovenUgdenbog).addComponent(this.cbCovenUgdenbog)).addGroup(layout.createParallelGroup().addComponent(this.lblBarrowholm).addComponent(this.cbBarrowholm)).addGroup(layout.createParallelGroup().addComponent(this.lblMalmouth).addComponent(this.cbMalmouth)).addGroup(layout.createParallelGroup().addComponent(this.lblCultBysmiel).addComponent(this.cbCultBysmiel)).addGroup(layout.createParallelGroup().addComponent(this.lblCultDreeg).addComponent(this.cbCultDreeg)).addGroup(layout.createParallelGroup().addComponent(this.lblCultSolael).addComponent(this.cbCultSolael)).addGroup(layout.createParallelGroup().addComponent(this.lblUndead).addComponent(this.cbUndead)).addGroup(layout.createParallelGroup().addComponent(this.lblBeasts).addComponent(this.cbBeasts)).addGroup(layout.createParallelGroup().addComponent(this.lblAetherials).addComponent(this.cbAetherials)).addGroup(layout.createParallelGroup().addComponent(this.lblChthonians).addComponent(this.cbChthonians)).addGroup(layout.createParallelGroup().addComponent(this.lblOutlaws).addComponent(this.cbOutlaws)).addGroup(layout.createParallelGroup().addComponent(this.lblAetherialVanguard).addComponent(this.cbAetherialVanguard)).addGroup(layout.createParallelGroup().addComponent(this.lblEldritchHorrors).addComponent(this.cbEldritchHorrors));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblSurvivors, this.lblRovers);
        layout.linkSize(0, this.lblSurvivors, this.lblHomestead);
        layout.linkSize(0, this.lblSurvivors, this.lblBlackLegion);
        layout.linkSize(0, this.lblSurvivors, this.lblDeathsVigil);
        layout.linkSize(0, this.lblSurvivors, this.lblKymonsChosen);
        layout.linkSize(0, this.lblSurvivors, this.lblOutcast);
        layout.linkSize(0, this.lblSurvivors, this.lblCovenUgdenbog);
        layout.linkSize(0, this.lblSurvivors, this.lblBarrowholm);
        layout.linkSize(0, this.lblSurvivors, this.lblMalmouth);
        layout.linkSize(0, this.lblSurvivors, this.lblCultBysmiel);
        layout.linkSize(0, this.lblSurvivors, this.lblCultDreeg);
        layout.linkSize(0, this.lblSurvivors, this.lblCultSolael);
        layout.linkSize(0, this.lblSurvivors, this.lblUndead);
        layout.linkSize(0, this.lblSurvivors, this.lblBeasts);
        layout.linkSize(0, this.lblSurvivors, this.lblAetherials);
        layout.linkSize(0, this.lblSurvivors, this.lblChthonians);
        layout.linkSize(0, this.lblSurvivors, this.lblOutlaws);
        layout.linkSize(0, this.lblSurvivors, this.lblAetherialVanguard);
        layout.linkSize(0, this.lblSurvivors, this.lblEldritchHorrors);
        layout.linkSize(0, this.lblSurvivors, this.cbSurvivors);
        layout.linkSize(0, this.lblSurvivors, this.cbRovers);
        layout.linkSize(0, this.lblSurvivors, this.cbHomestead);
        layout.linkSize(0, this.lblSurvivors, this.cbBlackLegion);
        layout.linkSize(0, this.lblSurvivors, this.cbDeathsVigil);
        layout.linkSize(0, this.lblSurvivors, this.cbKymonsChosen);
        layout.linkSize(0, this.lblSurvivors, this.cbOutcast);
        layout.linkSize(0, this.lblSurvivors, this.cbCovenUgdenbog);
        layout.linkSize(0, this.lblSurvivors, this.cbBarrowholm);
        layout.linkSize(0, this.lblSurvivors, this.cbMalmouth);
        layout.linkSize(0, this.lblSurvivors, this.cbCultBysmiel);
        layout.linkSize(0, this.lblSurvivors, this.cbCultDreeg);
        layout.linkSize(0, this.lblSurvivors, this.cbCultSolael);
        layout.linkSize(0, this.lblSurvivors, this.cbUndead);
        layout.linkSize(0, this.lblSurvivors, this.cbBeasts);
        layout.linkSize(0, this.lblSurvivors, this.cbAetherials);
        layout.linkSize(0, this.lblSurvivors, this.cbChthonians);
        layout.linkSize(0, this.lblSurvivors, this.cbOutlaws);
        layout.linkSize(0, this.lblSurvivors, this.cbAetherialVanguard);
        layout.linkSize(0, this.lblSurvivors, this.cbEldritchHorrors);
        layout.linkSize(1, this.lblSurvivors, this.lblRovers);
        layout.linkSize(1, this.lblSurvivors, this.lblHomestead);
        layout.linkSize(1, this.lblSurvivors, this.lblBlackLegion);
        layout.linkSize(1, this.lblSurvivors, this.lblDeathsVigil);
        layout.linkSize(1, this.lblSurvivors, this.lblKymonsChosen);
        layout.linkSize(1, this.lblSurvivors, this.lblOutcast);
        layout.linkSize(1, this.lblSurvivors, this.lblCovenUgdenbog);
        layout.linkSize(1, this.lblSurvivors, this.lblBarrowholm);
        layout.linkSize(1, this.lblSurvivors, this.lblMalmouth);
        layout.linkSize(1, this.lblSurvivors, this.lblCultBysmiel);
        layout.linkSize(1, this.lblSurvivors, this.lblCultDreeg);
        layout.linkSize(1, this.lblSurvivors, this.lblCultSolael);
        layout.linkSize(1, this.lblSurvivors, this.lblUndead);
        layout.linkSize(1, this.lblSurvivors, this.lblBeasts);
        layout.linkSize(1, this.lblSurvivors, this.lblAetherials);
        layout.linkSize(1, this.lblSurvivors, this.lblChthonians);
        layout.linkSize(1, this.lblSurvivors, this.lblOutlaws);
        layout.linkSize(1, this.lblSurvivors, this.lblAetherialVanguard);
        layout.linkSize(1, this.lblSurvivors, this.lblEldritchHorrors);
        layout.linkSize(1, this.lblSurvivors, this.cbSurvivors);
        layout.linkSize(1, this.lblSurvivors, this.cbRovers);
        layout.linkSize(1, this.lblSurvivors, this.cbHomestead);
        layout.linkSize(1, this.lblSurvivors, this.cbBlackLegion);
        layout.linkSize(1, this.lblSurvivors, this.cbDeathsVigil);
        layout.linkSize(1, this.lblSurvivors, this.cbKymonsChosen);
        layout.linkSize(1, this.lblSurvivors, this.cbOutcast);
        layout.linkSize(1, this.lblSurvivors, this.cbCovenUgdenbog);
        layout.linkSize(1, this.lblSurvivors, this.cbBarrowholm);
        layout.linkSize(1, this.lblSurvivors, this.cbMalmouth);
        layout.linkSize(1, this.lblSurvivors, this.cbCultBysmiel);
        layout.linkSize(1, this.lblSurvivors, this.cbCultDreeg);
        layout.linkSize(1, this.lblSurvivors, this.cbCultSolael);
        layout.linkSize(1, this.lblSurvivors, this.cbUndead);
        layout.linkSize(1, this.lblSurvivors, this.cbBeasts);
        layout.linkSize(1, this.lblSurvivors, this.cbAetherials);
        layout.linkSize(1, this.lblSurvivors, this.cbChthonians);
        layout.linkSize(1, this.lblSurvivors, this.cbOutlaws);
        layout.linkSize(1, this.lblSurvivors, this.cbAetherialVanguard);
        layout.linkSize(1, this.lblSurvivors, this.cbEldritchHorrors);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_FACTIONS"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        loadCBText();
        if (this.lblSurvivors == null) {
            this.lblSurvivors = new JLabel();
        }
        this.lblSurvivors.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_DEVILS_CROSSING"));
        this.lblSurvivors.setFont(fntLabel);
        if (this.lblRovers == null) {
            this.lblRovers = new JLabel();
        }
        this.lblRovers.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_ROVERS"));
        this.lblRovers.setFont(fntLabel);
        if (this.lblHomestead == null) {
            this.lblHomestead = new JLabel();
        }
        this.lblHomestead.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_HOMESTEAD"));
        this.lblHomestead.setFont(fntLabel);
        if (this.lblBlackLegion == null) {
            this.lblBlackLegion = new JLabel();
        }
        this.lblBlackLegion.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_BLACK_LEGION"));
        this.lblBlackLegion.setFont(fntLabel);
        if (this.lblDeathsVigil == null) {
            this.lblDeathsVigil = new JLabel();
        }
        this.lblDeathsVigil.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_DEATHS_VIGIL"));
        this.lblDeathsVigil.setFont(fntLabel);
        if (this.lblKymonsChosen == null) {
            this.lblKymonsChosen = new JLabel();
        }
        this.lblKymonsChosen.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_KYMONS_CHOSEN"));
        this.lblKymonsChosen.setFont(fntLabel);
        if (this.lblOutcast == null) {
            this.lblOutcast = new JLabel();
        }
        this.lblOutcast.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_OUTCAST"));
        this.lblOutcast.setFont(fntLabel);
        if (this.lblCovenUgdenbog == null) {
            this.lblCovenUgdenbog = new JLabel();
        }
        this.lblCovenUgdenbog.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_COVEN_UGDENBOG"));
        this.lblCovenUgdenbog.setFont(fntLabel);
        this.lblCovenUgdenbog.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.lblBarrowholm == null) {
            this.lblBarrowholm = new JLabel();
        }
        this.lblBarrowholm.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_BARROWHOLM"));
        this.lblBarrowholm.setFont(fntLabel);
        this.lblBarrowholm.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.lblMalmouth == null) {
            this.lblMalmouth = new JLabel();
        }
        this.lblMalmouth.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_MALMOUTH_RESISTANCE"));
        this.lblMalmouth.setFont(fntLabel);
        this.lblMalmouth.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.lblCultBysmiel == null) {
            this.lblCultBysmiel = new JLabel();
        }
        this.lblCultBysmiel.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_CULT_BYSMIEL"));
        this.lblCultBysmiel.setFont(fntLabel);
        this.lblCultBysmiel.setVisible(GDStashFrame.expansionForgottenGods);
        if (this.lblCultDreeg == null) {
            this.lblCultDreeg = new JLabel();
        }
        this.lblCultDreeg.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_CULT_DREEG"));
        this.lblCultDreeg.setFont(fntLabel);
        this.lblCultDreeg.setVisible(GDStashFrame.expansionForgottenGods);
        if (this.lblCultSolael == null) {
            this.lblCultSolael = new JLabel();
        }
        this.lblCultSolael.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_CULT_SOLAEL"));
        this.lblCultSolael.setFont(fntLabel);
        this.lblCultSolael.setVisible(GDStashFrame.expansionForgottenGods);
        if (this.lblUndead == null) {
            this.lblUndead = new JLabel();
        }
        this.lblUndead.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_UNDEAD"));
        this.lblUndead.setFont(fntLabel);
        if (this.lblBeasts == null) {
            this.lblBeasts = new JLabel();
        }
        this.lblBeasts.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_BEASTS"));
        this.lblBeasts.setFont(fntLabel);
        this.lblBeasts.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.lblAetherials == null) {
            this.lblAetherials = new JLabel();
        }
        this.lblAetherials.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_AETHERIALS"));
        this.lblAetherials.setFont(fntLabel);
        if (this.lblChthonians == null) {
            this.lblChthonians = new JLabel();
        }
        this.lblChthonians.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_CHTHONIANS"));
        this.lblChthonians.setFont(fntLabel);
        if (this.lblOutlaws == null) {
            this.lblOutlaws = new JLabel();
        }
        this.lblOutlaws.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_CRONLEYS_GANG"));
        this.lblOutlaws.setFont(fntLabel);
        if (this.lblAetherialVanguard == null) {
            this.lblAetherialVanguard = new JLabel();
        }
        this.lblAetherialVanguard.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_AETHERIAL_VANGUARD"));
        this.lblAetherialVanguard.setFont(fntLabel);
        this.lblAetherialVanguard.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.lblEldritchHorrors == null) {
            this.lblEldritchHorrors = new JLabel();
        }
        this.lblEldritchHorrors.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_ELDRITCH_HORRORS"));
        this.lblEldritchHorrors.setFont(fntLabel);
        this.lblEldritchHorrors.setVisible(GDStashFrame.expansionForgottenGods);
        if (this.cbSurvivors == null) {
            (this.cbSurvivors = new JComboBox<String>(GDCharFactionPane.factionPositive)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbSurvivors);
        }
        this.cbSurvivors.setFont(fntCombo);
        if (this.cbRovers == null) {
            (this.cbRovers = new JComboBox<String>(GDCharFactionPane.factionPositive)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbRovers);
        }
        this.cbRovers.setFont(fntCombo);
        if (this.cbHomestead == null) {
            (this.cbHomestead = new JComboBox<String>(GDCharFactionPane.factionPositive)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbHomestead);
        }
        this.cbHomestead.setFont(fntCombo);
        if (this.cbBlackLegion == null) {
            (this.cbBlackLegion = new JComboBox<String>(GDCharFactionPane.factionPositive)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbBlackLegion);
        }
        this.cbBlackLegion.setFont(fntCombo);
        if (this.cbDeathsVigil == null) {
            (this.cbDeathsVigil = new JComboBox<String>(GDCharFactionPane.factionAll)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbDeathsVigil);
        }
        this.cbDeathsVigil.setFont(fntCombo);
        if (this.cbKymonsChosen == null) {
            (this.cbKymonsChosen = new JComboBox<String>(GDCharFactionPane.factionAll)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbKymonsChosen);
        }
        this.cbKymonsChosen.setFont(fntCombo);
        if (this.cbOutcast == null) {
            (this.cbOutcast = new JComboBox<String>(GDCharFactionPane.factionAll)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbOutcast);
        }
        this.cbOutcast.setFont(fntCombo);
        if (this.cbCovenUgdenbog == null) {
            (this.cbCovenUgdenbog = new JComboBox<String>(GDCharFactionPane.factionAll)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbCovenUgdenbog);
        }
        this.cbCovenUgdenbog.setFont(fntCombo);
        this.cbCovenUgdenbog.setEnabled(GDStashFrame.expansionAshesOfMalmouth);
        this.cbCovenUgdenbog.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.cbBarrowholm == null) {
            (this.cbBarrowholm = new JComboBox<String>(GDCharFactionPane.factionAll)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbBarrowholm);
        }
        this.cbBarrowholm.setFont(fntCombo);
        this.cbBarrowholm.setEnabled(GDStashFrame.expansionAshesOfMalmouth);
        this.cbBarrowholm.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.cbMalmouth == null) {
            (this.cbMalmouth = new JComboBox<String>(GDCharFactionPane.factionAll)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbMalmouth);
        }
        this.cbMalmouth.setFont(fntCombo);
        this.cbMalmouth.setEnabled(GDStashFrame.expansionAshesOfMalmouth);
        this.cbMalmouth.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.cbCultBysmiel == null) {
            (this.cbCultBysmiel = new JComboBox<String>(GDCharFactionPane.factionPositive)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbCultBysmiel);
        }
        this.cbCultBysmiel.setFont(fntCombo);
        this.cbCultBysmiel.setEnabled(GDStashFrame.expansionForgottenGods);
        this.cbCultBysmiel.setVisible(GDStashFrame.expansionForgottenGods);
        if (this.cbCultDreeg == null) {
            (this.cbCultDreeg = new JComboBox<String>(GDCharFactionPane.factionPositive)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbCultDreeg);
        }
        this.cbCultDreeg.setFont(fntCombo);
        this.cbCultDreeg.setEnabled(GDStashFrame.expansionForgottenGods);
        this.cbCultDreeg.setVisible(GDStashFrame.expansionForgottenGods);
        if (this.cbCultSolael == null) {
            (this.cbCultSolael = new JComboBox<String>(GDCharFactionPane.factionPositive)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbCultSolael);
        }
        this.cbCultSolael.setFont(fntCombo);
        this.cbCultSolael.setEnabled(GDStashFrame.expansionForgottenGods);
        this.cbCultSolael.setVisible(GDStashFrame.expansionForgottenGods);
        if (this.cbUndead == null) {
            (this.cbUndead = new JComboBox<String>(GDCharFactionPane.factionNegative)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbUndead);
        }
        this.cbUndead.setFont(fntCombo);
        if (this.cbBeasts == null) {
            (this.cbBeasts = new JComboBox<String>(GDCharFactionPane.factionNegative)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbBeasts);
        }
        this.cbBeasts.setFont(fntCombo);
        this.cbBeasts.setEnabled(GDStashFrame.expansionAshesOfMalmouth);
        this.cbBeasts.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.cbAetherials == null) {
            (this.cbAetherials = new JComboBox<String>(GDCharFactionPane.factionNegative)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbAetherials);
        }
        this.cbAetherials.setFont(fntCombo);
        if (this.cbChthonians == null) {
            (this.cbChthonians = new JComboBox<String>(GDCharFactionPane.factionNegative)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbChthonians);
        }
        this.cbChthonians.setFont(fntCombo);
        if (this.cbOutlaws == null) {
            (this.cbOutlaws = new JComboBox<String>(GDCharFactionPane.factionNegative)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbOutlaws);
        }
        this.cbOutlaws.setFont(fntCombo);
        if (this.cbAetherialVanguard == null) {
            (this.cbAetherialVanguard = new JComboBox<String>(GDCharFactionPane.factionNegative)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbAetherialVanguard);
        }
        this.cbAetherialVanguard.setFont(fntCombo);
        this.cbAetherialVanguard.setEnabled(GDStashFrame.expansionAshesOfMalmouth);
        this.cbAetherialVanguard.setVisible(GDStashFrame.expansionAshesOfMalmouth);
        if (this.cbEldritchHorrors == null) {
            (this.cbEldritchHorrors = new JComboBox<String>(GDCharFactionPane.factionNegative)).addActionListener(new ReputationListener());
        } else {
            this.updateLanguage(this.cbEldritchHorrors);
        }
        this.cbEldritchHorrors.setFont(fntCombo);
        this.cbEldritchHorrors.setEnabled(GDStashFrame.expansionForgottenGods);
        this.cbEldritchHorrors.setVisible(GDStashFrame.expansionForgottenGods);
    }

    private void updateLanguage(final JComboBox<String> cb) {
        this.skipListener = true;
        for (int i = 0; i < this.cbReputations.length; ++i) {
            if (cb == this.cbReputations[i]) {
                final int index = cb.getSelectedIndex();
                cb.removeAllItems();
                String[] factions = GDCharFactionPane.factionAll;
                switch (this.reputationTypes[i]) {
                    case 2: {
                        factions = GDCharFactionPane.factionPositive;
                        break;
                    }
                    case 3: {
                        factions = GDCharFactionPane.factionNegative;
                        break;
                    }
                }
                for (int j = 0; j < factions.length; ++j) {
                    cb.addItem(factions[j]);
                }
                cb.setSelectedIndex(index);
            }
        }
        this.skipListener = false;
    }

    private void fillReputation(final JComboBox cb) {
        final int index = cb.getSelectedIndex();
        for (int i = 0; i < this.cbReputations.length; ++i) {
            if (cb == this.cbReputations[i]) {
                switch (this.reputationTypes[i]) {
                    case 1: {
                        this.reputations[i] = this.getReputationAll(index);
                        break;
                    }
                    case 2: {
                        this.reputations[i] = this.getReputationPositive(index);
                        break;
                    }
                    case 3: {
                        this.reputations[i] = this.getReputationNegative(index);
                        break;
                    }
                }
                return;
            }
        }
    }

    private int getReputationAll(final int index) {
        return GDCharFactionPane.minReputationLevels[index];
    }

    private int getReputationNegative(final int index) {
        return GDCharFactionPane.minReputationLevels[4 - index];
    }

    private int getReputationPositive(final int index) {
        return GDCharFactionPane.minReputationLevels[index + 4];
    }

    private void setReputationAll(final JComboBox cb, final int reputation) {
        for (int i = 0; i < 4; ++i) {
            if (reputation <= GDCharFactionPane.minReputationLevels[i]) {
                cb.setSelectedIndex(i);
                return;
            }
        }
        for (int i = 9; i > 4; --i) {
            if (reputation >= GDCharFactionPane.minReputationLevels[i]) {
                cb.setSelectedIndex(i);
                return;
            }
        }
        cb.setSelectedIndex(4);
    }

    private void setReputationNegative(final JComboBox cb, final int reputation) {
        for (int i = 0; i < 4; ++i) {
            if (reputation <= GDCharFactionPane.minReputationLevels[i]) {
                cb.setSelectedIndex(4 - i);
                return;
            }
        }
        cb.setSelectedIndex(0);
    }

    private void setReputationPositive(final JComboBox cb, final int reputation) {
        for (int i = 9; i > 4; --i) {
            if (reputation >= GDCharFactionPane.minReputationLevels[i]) {
                cb.setSelectedIndex(i - 4);
                return;
            }
        }
        cb.setSelectedIndex(0);
    }

    private void adjustReputationValues(final int faction, final JComboBox<String> jcb) {
        this.reputationTypes[faction] = 1;
        if (this.reputations[faction] < 0) {
            this.reputationTypes[faction] = 3;
        }
        if (this.reputations[faction] > 0) {
            this.reputationTypes[faction] = 2;
        }
        DefaultComboBoxModel<String> model = null;
        switch (this.reputationTypes[faction]) {
            case 3: {
                model = new DefaultComboBoxModel<String>(GDCharFactionPane.factionNegative);
                break;
            }
            case 2: {
                model = new DefaultComboBoxModel<String>(GDCharFactionPane.factionPositive);
                break;
            }
            default: {
                model = new DefaultComboBoxModel<String>(GDCharFactionPane.factionAll);
                break;
            }
        }
        jcb.setModel(model);
        switch (this.reputationTypes[faction]) {
            case 3: {
                this.setReputationNegative(jcb, this.reputations[faction]);
                break;
            }
            case 2: {
                this.setReputationPositive(jcb, this.reputations[faction]);
                break;
            }
            default: {
                this.setReputationAll(jcb, this.reputations[faction]);
                break;
            }
        }
    }

    public void setChar(final GDChar gdc) {
        this.skipListener = true;
        boolean expAoM = GDStashFrame.expansionAshesOfMalmouth;
        boolean expFG = GDStashFrame.expansionForgottenGods;
        if (gdc == null) {
            for (int i = 0; i < this.reputations.length; ++i) {
                this.reputations[i] = 0;
            }
        } else {
            this.reputations = gdc.getFactionReputations();
            expFG = gdc.isForgottenGodsChar();
            expAoM = (expFG || gdc.isAsheshOfMalmouthChar());
        }
        if (!expAoM) {
            GDCharFactionPane.FACTIONS = GDCharFactionPane.FACTIONS_VANILLA;
        } else if (expFG) {
            GDCharFactionPane.FACTIONS = GDCharFactionPane.FACTIONS_GODS;
        } else {
            GDCharFactionPane.FACTIONS = GDCharFactionPane.FACTIONS_MALMOUTH;
        }
        this.lblCovenUgdenbog.setVisible(expAoM);
        this.cbCovenUgdenbog.setEnabled(expAoM);
        this.cbCovenUgdenbog.setVisible(expAoM);
        this.lblBarrowholm.setVisible(expAoM);
        this.cbBarrowholm.setEnabled(expAoM);
        this.cbBarrowholm.setVisible(expAoM);
        this.lblMalmouth.setVisible(expAoM);
        this.cbMalmouth.setEnabled(expAoM);
        this.cbMalmouth.setVisible(expAoM);
        this.lblBeasts.setVisible(expAoM);
        this.cbBeasts.setEnabled(expAoM);
        this.cbBeasts.setVisible(expAoM);
        this.lblAetherialVanguard.setVisible(expAoM);
        this.cbAetherialVanguard.setEnabled(expAoM);
        this.cbAetherialVanguard.setVisible(expAoM);
        this.lblCultBysmiel.setVisible(expFG);
        this.cbCultBysmiel.setEnabled(expFG);
        this.cbCultBysmiel.setVisible(expFG);
        this.lblCultDreeg.setVisible(expFG);
        this.cbCultDreeg.setEnabled(expFG);
        this.cbCultDreeg.setVisible(expFG);
        this.lblCultSolael.setVisible(expFG);
        this.cbCultSolael.setEnabled(expFG);
        this.cbCultSolael.setVisible(expFG);
        this.lblEldritchHorrors.setVisible(expFG);
        this.cbEldritchHorrors.setEnabled(expFG);
        this.cbEldritchHorrors.setVisible(expFG);
        final DefaultComboBoxModel<String> model = null;
        this.setReputationPositive(this.cbSurvivors, this.reputations[0]);
        this.setReputationPositive(this.cbRovers, this.reputations[1]);
        this.setReputationPositive(this.cbHomestead, this.reputations[2]);
        this.setReputationPositive(this.cbBlackLegion, this.reputations[3]);
        this.setReputationAll(this.cbDeathsVigil, this.reputations[4]);
        this.setReputationAll(this.cbKymonsChosen, this.reputations[5]);
        this.setReputationAll(this.cbOutcast, this.reputations[6]);
        this.setReputationPositive(this.cbCovenUgdenbog, this.reputations[7]);
        this.setReputationAll(this.cbBarrowholm, this.reputations[8]);
        this.setReputationPositive(this.cbMalmouth, this.reputations[9]);
        this.setReputationPositive(this.cbCultBysmiel, this.reputations[10]);
        this.setReputationPositive(this.cbCultDreeg, this.reputations[11]);
        this.setReputationPositive(this.cbCultSolael, this.reputations[12]);
        this.setReputationNegative(this.cbUndead, this.reputations[13]);
        this.setReputationNegative(this.cbBeasts, this.reputations[14]);
        this.setReputationNegative(this.cbAetherials, this.reputations[15]);
        this.setReputationNegative(this.cbChthonians, this.reputations[16]);
        this.setReputationNegative(this.cbOutlaws, this.reputations[17]);
        this.setReputationNegative(this.cbAetherialVanguard, this.reputations[18]);
        this.setReputationNegative(this.cbEldritchHorrors, this.reputations[19]);
        this.skipListener = false;
    }

    public void updateChar(final GDChar gdc) {
        if (gdc == null) {
            return;
        }
        gdc.setFactionReputations(this.reputations);
    }

    public void updateConfig() {
        initReputationLevels();
        loadCBText();
    }

    private static void loadCBText() {
        GDCharFactionPane.factionAll[0] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_NEMESIS");
        GDCharFactionPane.factionAll[1] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_HATED");
        GDCharFactionPane.factionAll[2] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_DESPISED");
        GDCharFactionPane.factionAll[3] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_HOSTILE");
        GDCharFactionPane.factionAll[4] = "";
        GDCharFactionPane.factionAll[5] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_TOLERATED");
        GDCharFactionPane.factionAll[6] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_FRIENDLY");
        GDCharFactionPane.factionAll[7] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_RESPECTED");
        GDCharFactionPane.factionAll[8] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_HONORED");
        GDCharFactionPane.factionAll[9] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "FACTION_STATUS_REVERED");
        GDCharFactionPane.factionNegative[0] = GDCharFactionPane.factionAll[4];
        GDCharFactionPane.factionNegative[1] = GDCharFactionPane.factionAll[3];
        GDCharFactionPane.factionNegative[2] = GDCharFactionPane.factionAll[2];
        GDCharFactionPane.factionNegative[3] = GDCharFactionPane.factionAll[1];
        GDCharFactionPane.factionNegative[4] = GDCharFactionPane.factionAll[0];
        GDCharFactionPane.factionPositive[0] = GDCharFactionPane.factionAll[4];
        GDCharFactionPane.factionPositive[1] = GDCharFactionPane.factionAll[5];
        GDCharFactionPane.factionPositive[2] = GDCharFactionPane.factionAll[6];
        GDCharFactionPane.factionPositive[3] = GDCharFactionPane.factionAll[7];
        GDCharFactionPane.factionPositive[4] = GDCharFactionPane.factionAll[8];
        GDCharFactionPane.factionPositive[5] = GDCharFactionPane.factionAll[9];
    }

    private class ReputationListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDCharFactionPane.this.skipListener) {
                return;
            }
            final JComboBox source = (JComboBox) e.getSource();
            GDCharFactionPane.this.fillReputation(source);
        }
    }
}
